package com.smy.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smy.reggie.common.R;
import com.smy.reggie.pojo.User;
import com.smy.reggie.service.UserService;
import com.smy.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //private Integer code;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/getCode")
    public R<String> getCode(@RequestBody User user, HttpSession session){
        log.warn("获取验证码");
        //获取手机号
        String phone = user.getPhone();
        //如果手机号存在
        if(!StringUtils.isEmpty(phone)){
            //生成6位验证码
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            //日志输出验证码
            log.warn(code + "");

            //调用互亿api发送验证码
            //sendsmsutf8.SEED(phone,code);

            //将验证码存入session
            //session.setAttribute(phone,code);

            //将验证码存入redis
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("发送成功");
        }
        return R.error("发送失败");
    }

    //登录
    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpSession session){
        log.warn("进入登录");
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();

        //将验证码从session中取出
        //String codeInSession = session.getAttribute(phone).toString();

        //将验证码从Redis中取出
        String codeInSession = redisTemplate.opsForValue().get(phone).toString();
        //比对验证码是否相同
        if(codeInSession != null && codeInSession.equals(code)){
            //判断用户是否已经注册
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone,phone);
            User user = userService.getOne(wrapper);
            if(user == null ){//未注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }else {//已经注册
                if(user.getStatus() == 0){//已禁用
                    return R.error("该用户已被禁用！");
                }
            }
            //成功登录,将该用户存入session
            session.setAttribute("user",user.getId());
            //删除redis中的验证码
            redisTemplate.delete(phone);
            return R.success("登录成功！");
        }
        //验证码不对
        return R.error("验证码错误！");
    }

}
